package com;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author : gameZhao
 * @version V1.0
 * @Project: disruptorDemo
 * @Package PACKAGE_NAME
 * @Description: 线程处理器工厂
 * @date Date : 2021年12月27日 11:29
 */
public class ProcessorThreadFactory implements ThreadFactory {
    private static final AtomicInteger poolNumber = new AtomicInteger(1);
    private final ThreadGroup group;
    private final String processorName;
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    
    public static ProcessorThreadFactory create(String processorName){
        return new ProcessorThreadFactory(processorName);
    }
    
    ProcessorThreadFactory(String processorName) {
        SecurityManager s = System.getSecurityManager();
        group = (s != null) ? s.getThreadGroup() :
                Thread.currentThread().getThreadGroup();
       this.processorName = processorName;
    }
    
    public Thread newThread(Runnable r) {
        Thread t = new Thread(group, r,
                processorName,
                0);
        if (t.isDaemon())
            t.setDaemon(false);
        if (t.getPriority() != Thread.NORM_PRIORITY)
            t.setPriority(Thread.NORM_PRIORITY);
        return t;
    }
}
